// component/preview/preview.js
var app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    docData: {
      type: Object,
      value: {}
    },
    isDeletable: {
      type: Boolean,
      value: false
    },
    type: {
      type: String,
      value: 'supply'
    },
    isShow: {
      type: Boolean,
      value: false
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    isfav: false
  },
  lifetimes: {
    attached() {
      app.globalData.favorities.map((res) => {
        if (this.data.docData.id == res.id) {
          this.setData({
            isfav: true
          })
        }
        return 0
      })
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    deleteItem: async function () {
      wx.showModal({
        title: "确定删除吗？",
      }).then(async (res) => {
        if (res.confirm) {
          console.log("删除上传的文件");
          let res = await app.delete(`files/${this.data.docData.id}/`, {
            "file_id": this.data.docData.id
          })
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 1000
          })
          this.triggerEvent("deleted")
        }
      })
    },
    gotodetail: function (e) {
      let that = this
      wx.navigateTo({
        url: '/pages/detail/detail?detail=' + JSON.stringify(e.currentTarget.dataset.info),
        success(res){
          res.eventChannel.on('changefav',(data) =>{
            that.setData({
              'isfav': data
            })
          })
        }
      })
      
    },

    async changefav() {
      let res = await app.changeFav(this.data.isfav, this.data.docData)
      this.setData({
        isfav: this.data.isfav ^ res
      })
    }


  }
})